package com.modules.asset.controller;

import com.modules.asset.model.dto.UserCreateRequest;
import com.modules.asset.model.dto.UserResponse;
import com.modules.asset.model.dto.UserUpdateRequest;
import com.modules.asset.service.UserService;
import com.modules.asset.common.BaseResponse;
import com.modules.asset.common.ResultUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * User 控制器。
 */
@RestController
@RequestMapping("/asset")
@RequiredArgsConstructor
@Validated
@Tag(name = "User管理", description = "User 基础增删改查接口")
public class UserController {

    private final UserService assetService;

    @PostMapping
    @Operation(summary = "创建User")
    public BaseResponse<UserResponse> create(@RequestBody @Valid UserCreateRequest request) {
        return ResultUtils.success(assetService.create(request));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新User")
    public BaseResponse<UserResponse> update(@PathVariable Long id,
                                                              @RequestBody @Valid UserUpdateRequest request) {
        return ResultUtils.success(assetService.update(id, request));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除User")
    public BaseResponse<Boolean> delete(@PathVariable Long id) {
        assetService.delete(id);
        return ResultUtils.success(Boolean.TRUE);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询User详情")
    public BaseResponse<UserResponse> detail(@PathVariable Long id) {
        return ResultUtils.success(assetService.findById(id));
    }

    @GetMapping
    @Operation(summary = "查询User列表")
    public BaseResponse<List<UserResponse>> list() {
        return ResultUtils.success(assetService.listAll());
    }
}
